import { Component } from 'react';
import { Button, Card, Input, Form, Checkbox, message } from 'antd';
import styles from './index.module.scss';
import ImgSrc from 'assets/logo.png';
import { loginApi } from 'api/login';

export default class Login extends Component {
  onFinish = async (value) => {
    console.log('value  ----->  ', value);
    try {
      const res = await loginApi(value);
      localStorage.setItem('geek-pack-token', res.data.token);
      message.success('登录成功');
      this.props.history.push('/layout');
    } catch (error) {
      // console.log('error  ----->  ', error);
      console.dir('error  ----->  ', error.response);
      message.warning(error.response.data.message);
    }
  };
  render() {
    return (
      <div className={styles.login}>
        <Card className="login-container">
          <img src={ImgSrc} alt="" className="login-logo" />
          <Form
            onFinish={this.onFinish}
            // onFinishFailed={onFinishFailed}
            autoComplete="off"
            initialValues={{
              agree: true,
              mobile: '13811111111',
              code: '246810',
            }}
          >
            <Form.Item
              name="mobile"
              rules={[
                {
                  required: true,
                  message: '手机号不能为空!',
                },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: '请输入合法的11位手机号码!',
                },
              ]}
            >
              <Input />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                {
                  required: true,
                  message: '验证码不能为空!',
                },
                {
                  pattern: /^\d{6}$/,
                  message: '请输入6位验证码!',
                },
              ]}
            >
              <Input />
            </Form.Item>

            <Form.Item
              valuePropName="checked"
              name="agree"
              rules={[
                {
                  validator: (_, value) => {
                    if (value === true) {
                      return Promise.resolve();
                    } else {
                      return Promise.reject(
                        new Error('请阅读相关协议,同意后勾选')
                      );
                    }
                  },
                },
              ]}
            >
              <Checkbox>我已阅读并同意[隐私条款]和[用户协议]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button block type="primary" htmlType="submit">
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    );
  }
}
